﻿using ConfExpert.Forms.Base;
using DevExpress.Data;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Depo._7C
{
    public partial class KalanModeller : XForm
    {
        int CKBatchId = 0;
        public KalanModeller(int ckBatchId)
        {
            InitializeComponent();
            this.CKBatchId = ckBatchId;
        }

        DataTable dtDynamic = new DataTable("Dynamic");
        DataTable dtModel, dtExec, dtNeed, dtDepo;
        private void KalanModeller_Load(object sender, EventArgs e)
        {
            
            dtDynamic.Columns.Add("ProductId", typeof(string));
            dtDynamic.Columns.Add("QtyAx", typeof(decimal));
            dtDynamic.Columns.Add("QtyGx", typeof(decimal));
            dtDynamic.Columns.Add("Qty", typeof(decimal), "ISNULL(QtyAx,0)+ISNULL(QtyGx,0)");
            dtDynamic.Columns.Add("QtyAyrilan", typeof(decimal));
            dtDynamic.Columns.Add("QtyFARK", typeof(decimal), "ISNULL(QtyAyrilan,0)-ISNULL(Qty,0)");
            dtDynamic.PrimaryKey = new DataColumn[] { dtDynamic.Columns["ProductId"] };

            DataSet d = ExecuteDataSet("GetModelKalan", CKBatchId);
            dtModel = d.Tables[0].Copy();
            dtExec = d.Tables[1].Copy();
            dtNeed = d.Tables[2].Copy();
            dtDepo = d.Tables[3].Copy();
            dtDepo.PrimaryKey = new DataColumn[] { dtDepo.Columns["ProductId"] };

            gridView1.Init(dtModel);
            gridView2.Init(dtDynamic);
            gridView3.Init(dtExec);

            StyleFormatCondition condition1 = new DevExpress.XtraGrid.StyleFormatCondition();
            condition1.Appearance.ForeColor = Color.Crimson;
            condition1.Appearance.Options.UseForeColor = true;
            condition1.Condition = FormatConditionEnum.Less;
            condition1.Value1 = 0;
            gridView2.FormatConditions.Add(condition1);

            gridView1.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDown;
            gridView1.SelectionChanged += gridView1_SelectionChanged;
            gridView1.YanYanaGetir("Sel:Model");

            gridView1.FormatAsNumber(0, "Adet");
            gridView2.FormatAsNumber(1, "QtyAx", "QtyGx", "Qty", "QtyAyrilan", "QtyFARK");
            gridView3.FormatAsNumber(0, "Adet");

            gridView1.AddSummary("Adet");
            gridView2.AddSummary("QtyAx", "QtyGx", "Qty", "QtyAyrilan", "QtyFARK");
            gridView3.AddSummary("Adet");

            gridView1.MakeReadOnly("Sel");
            gridView2.MakeReadOnly();
            gridView3.MakeReadOnly();

            splitContainerControl1.AutoSize();

            gridView1.BestFitColumns();
            gridView3.BestFitColumns();
        }

        void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            GridView g = sender as GridView;
            DevExpress.XtraGrid.Views.Base.GridCell[] cells = g.GetSelectedCells();
            if (cells.Length == 0)
                return;

            int Adet = 0;
            List<string> Models = new List<string>();
            foreach (DevExpress.XtraGrid.Views.Base.GridCell gc in cells)
            {
                string Model = g.GetRowCellDisplayText(gc.RowHandle, "Model");
                if (!Models.Contains(Model))
                {
                    Models.Add(Model);
                    Adet += g.GetRowCellValue(gc.RowHandle, "Adet").ToInt();
                }
            }
            Notify("Seçilen modellerdeki toplam gömlek adedi: " + Adet);

            gridControl3.DataSource = null;
            if (Models.Count > 0)
                gridControl3.DataSource = dtExec.Select("Model='" + string.Join("' OR Model='", Models.ToArray()) + "'").CopyToDataTable();

            dtDynamic.Clear();
            foreach (string Model in Models)
            {
                foreach (DataRow dr in dtNeed.Select("Model='" + Model + "'"))
                {
                    int ProductId = dr["ProductId"].ToInt();
                    if (dtDynamic.Rows.Find(ProductId) == null)
                    {
                        DataRow r = dtDynamic.NewRow();
                        r["ProductId"] = ProductId;
                        r["QtyAyrilan"] = dtDepo.Rows.Find(ProductId)["QtyDepo"];
                        dtDynamic.Rows.Add(r);
                    }

                    DataRow rx = dtDynamic.Rows.Find(ProductId);
                    rx["QtyAx"] = rx["QtyAx"].ToDecimal() + dr["QtyAx"].ToDecimal();
                    rx["QtyGx"] = rx["QtyGx"].ToDecimal() + dr["QtyGx"].ToDecimal();
                }
            }

            gridView2.BeginSort();
            gridView2.Columns["QtyFARK"].SortOrder = ColumnSortOrder.Ascending;
            gridView2.EndSort();

            gridView2.BestFitColumns();
        }
    }
}
